
import { addSeck, changeSeck, delSeck, getGoodsList, getSeck, oneSeck } from "../../request/api";
import { successAlert } from '../../utils/alert'
const state = {
    tableData: [],//列表
    form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",//商品一级分类编号
        second_cateid: "",//商品二级分类编号
        goodsid: "",//商品编号
        status: 1
    },
    time:[]
}

// 2,声明mutations
const mutations = {
    // 专门来修改goods数据
    changeseck(state, obj) {
        state.tableData = obj;
    },
    editform(state, form) {
        state.form = form
    },
    edittime(state, time) {
        state.time = time
    },

}

// 3.声明actions
const actions = {
    seckActions(context) {
        getSeck().then(res=>{
            if(res.code==200){
                // console.log(res.list);
                context.commit('changeseck',res.list)
            }
        })
    },
    // oneActions(context, id) {
    //     oneSeck({id}).then(res=>{
    //         if(res.code==200){
    //             // res.list.id = id;
    //             console.log(res.list);
    //             let a=[res.list.begintime,res.list.endtime]
    //             context.commit('editform',res.list)
    //             context.commit('edittime',a)

    //         }
    //     })
    // },
    formAction(context,obj){
        context.commit('editform',obj)
    },
    updateActions(context, obj) {
        changeSeck(obj).then(res=>{
            if(res.code==200){
                successAlert(res.msg)
                context.dispatch('seckActions')
            }
        })
    },
    addActions(context, obj) {
        // console.log(obj);
        addSeck(obj).then(res=>{
            // console.log(res);
            if(res.code==200){
                // console.log(res);
                successAlert(res.msg)
                context.dispatch('seckActions')
            }
        })
    },
    clearActions(context) {
        let form = {
            title: "",
            begintime: "",
            endtime: "",
            first_cateid: "",//商品一级分类编号
            second_cateid: "",//商品二级分类编号
            goodsid: "",//商品编号
            status: 1
        }
        context.commit('editform', form)
    },
    delActions(context, id) {
        delSeck({id}).then(res=>{
            if(res.code==200){
                successAlert(res.msg)
                context.dispatch('seckActions')
            }
        })
    },


}
// 4.声明getters
const getters = {
    tableData(state) {
        return state.tableData
    },
    form(state) {
        return state.form
    },
    time(state) {
        return state.time
    },
}

export default {
    state,
    mutations,
    actions, getters,
    namespaced: true,//使用命名
}